package cn.sh.library.mgts.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.sh.library.mgts.bean.ResponseBean;
import cn.sh.library.mgts.bean.SearchMap;
import cn.sh.library.mgts.common.Constant;
import cn.sh.library.mgts.dto.Pager;
import cn.sh.library.mgts.service.HorizonService;

@Controller
@RequestMapping("/horizon")
public class BibHorizonController {

	@Autowired
	HorizonService horizonService;

	private static Logger loggerAccess = LoggerFactory.getLogger(Constant.LOGGER_ACCESS_NAME);
	
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/search")
	public ResponseBean<Object> search(@RequestBody SearchMap searchMap) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Pager pager = searchMap.getPager();
		
		List<Map<String, String>> res = horizonService.searchQuery(searchMap, pager);
		resMap.put("pager", pager);
		resMap.put("datas", res);
		
		loggerAccess.info(searchMap.toString());
		return new ResponseBean<Object>(true, resMap);
	}
	
	//根据bibno获取bib的数据    @PathVariable变量标签
	@ResponseBody
	@RequestMapping(method = RequestMethod.GET, value = "{bibno}")
	public ResponseBean<Object>  bibSearch(@PathVariable("bibno")  String bibno) {
		String bib = "bib_"+bibno;
		Map<String, Object> res = horizonService.bibQuery(bib);
		return  new ResponseBean<Object>(true, res);
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/freesearch")
	public ResponseBean<Object> freesearch(@RequestBody SearchMap searchMap) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Pager pager = searchMap.getPager();
		
		List<Map<String, String>> res = horizonService.freetextSearch(searchMap, pager);
		resMap.put("pager", pager);
		resMap.put("datas", res);
		
		loggerAccess.info(searchMap.toString());
		return new ResponseBean<Object>(true, resMap);
	}

	@ResponseBody
	@RequestMapping(method = RequestMethod.POST, value = "/freefacet")
	public ResponseBean<Object> freefacet(@RequestBody SearchMap searchMap) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		Map<String, Object> res = horizonService.freetextFacet(searchMap);
		resMap.put("datas", res);
		
		loggerAccess.info(searchMap.toString());
		return new ResponseBean<Object>(true, resMap);
	}
	
}
